package com.todo.pro.dt;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;

import com.todo.pro.dt.external.redis.RedisDtClientConfig;
import com.todo.pro.dt.external.redis.RedisDtClientFactory;

public class InitDtClientFactory implements InitializingBean, DisposableBean {

	private Logger logger = LoggerFactory.getLogger(InitDtClientFactory.class);

	@Value("${redis.host}")
	private String redisHost;
	@Value("${redis.port}")
	private int redisPort;
	@Value("${redis.auth}")
	private String redisAuth;

	@Override
	public void afterPropertiesSet() throws Exception {
		logger.info("Start Init DtClientFactory...");
		RedisDtClientConfig config = new RedisDtClientConfig();
		config.setHost(redisHost);
		config.setPort(redisPort);
		config.setAuth(redisAuth);
		RedisDtClientFactory redisDtClientFactory = new RedisDtClientFactory();
		redisDtClientFactory.setConfig(config);
		redisDtClientFactory.init();
		Dt.getInstance().setDefaultDtClientFactory(redisDtClientFactory);
		logger.info("Init DtClientFactory Suc");
	}

	@Override
	public void destroy() throws Exception {
		Dt.getInstance().getDefaultDtClientFactory().destroy();
		logger.info("DtClientFactory destroy");
	}

}
